package org.xxpay.manage.config.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.AppUser;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: zhuxiao
 * @date: 19/03/12
 * @description: 设备用户
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/app_user")
public class AppUserController extends BaseController {

    /**
     * 查询设备用户列表
     * @param request
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        AppUser appUser = getObject(param, AppUser.class);
        int count = commonService.appUserService.count(appUser);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<AppUser> appUserList = commonService.appUserService.select((getPageIndex(param) - 1) * getPageSize(param), getPageSize(param), appUser);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(appUserList, count));
    }

    /**
     * 查询设备用户信息
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long userId = getLongRequired(param, "userId");
        AppUser appUser = commonService.appUserService.findByUserId(userId);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(appUser));
    }

    /**
     * 新增设备用户信息
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        AppUser appUser = getObject(param, AppUser.class);
        if(appUser.getPassword() == null || appUser.getPassword().length() > 64){
            return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_MGR_PASSWORD_FORMAT_FAIL));
        }
        appUser.setStatus(Constant.PUB_YES);
        appUser.setAppStatus(Constant.PUB_NO);
        int count = commonService.appUserService.add(appUser);
        if(count != 1) ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

    /**
     * 修改设备用户信息
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        AppUser appUser = getObject(param, AppUser.class);
        if(appUser.getPassword() != null && appUser.getPassword().length() > 64){
            return ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_MGR_PASSWORD_FORMAT_FAIL));
        }
        int count = commonService.appUserService.update(appUser);
        if(count != 1) ResponseEntity.ok(XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

}
